package cn.personal.study.springbootdemo.web.rest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.personal.study.springbootdemo.domain.dto.request.CreateStudentRequestDTO;
import cn.personal.study.springbootdemo.domain.dto.request.QueryStudentRequestDTO;
import cn.personal.study.springbootdemo.domain.dto.request.UpdateStudentRequestDTO;
import cn.personal.study.springbootdemo.domain.dto.response.CreateStudentResponseDTO;
import cn.personal.study.springbootdemo.domain.dto.response.QueryStudentResponseDTO;
import cn.personal.study.springbootdemo.domain.dto.response.UpdateStudentResponseDTO;
import cn.personal.study.springbootdemo.domain.dto.response.ViewStudentResponseDTO;
import cn.personal.study.springbootdemo.service.StudentService;

/**
 * 学生资源类（控制器）
 *
 * @author duliurui
 * @since 1.0.0
 */
@RestController
@RequestMapping("/api/students")
public class StudentResource {

    @Autowired
    private StudentService studentService;

    @PostMapping
    public ResponseEntity<CreateStudentResponseDTO> createStudent(@RequestBody CreateStudentRequestDTO requestDTO) {
        CreateStudentResponseDTO student = studentService.createStudent(requestDTO);

        return ResponseEntity.ok(student);
    }

    @GetMapping(value = "/{studentId}")
    public ResponseEntity<ViewStudentResponseDTO> viewStudent(@PathVariable("studentId") String studentId) {
        ViewStudentResponseDTO student = studentService.viewStudent(studentId);

        return ResponseEntity.ok(student);
    }

    @PutMapping(value = "/{studentId}")
    public ResponseEntity<UpdateStudentResponseDTO> updateStudent(@PathVariable("studentId") String studentId,
                                                                  @RequestBody UpdateStudentRequestDTO requestDTO) {
        requestDTO.setId(studentId);
        return ResponseEntity.ok(studentService.updateStudent(requestDTO));
    }

    @DeleteMapping(value = "/{studentId}")
    public ResponseEntity<Void> deleteStudent(@PathVariable("studentId") String studentId) {
        studentService.delete(studentId);
        return ResponseEntity.noContent().build();
    }

    @PostMapping(value = "/query")
    public ResponseEntity<Page<QueryStudentResponseDTO>> queryStudent(@RequestBody QueryStudentRequestDTO requestDTO,
                                                                      Pageable pageable) {
        return ResponseEntity.ok(studentService.queryStudent(requestDTO, pageable));
    }
}
